package com.zm.schedule.schedulemgr.service.impl;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zm.base.core.dao.IBaseDao;
import com.zm.base.core.exception.BaseException;
import com.zm.schedule.schedulemgr.dao.IScheduleDao;
import com.zm.schedule.schedulemgr.po.TbZmScheduleJob;
import com.zm.schedule.schedulemgr.po.TbZmScheduleJobLog;
import com.zm.schedule.schedulemgr.service.IScheduleService;
import com.zm.schedule.schedulemgr.service.ISchedulerBuilder;
import com.zm.schedule.schedulemgr.vo.TbZmScheduleJobVO;

/**
 * @author chenhuaming
 * @Time：2017年5月3日 下午1:10:52
 * @version 1.0
 */
@Service("scheduleService")
public class ScheduleService implements IScheduleService {
	@Resource(name="scheduleDao")
	private IScheduleDao scheduleDao;
	@Resource(name="baseDao")
	private IBaseDao baseDao; //主要用于写操作
	@Resource(name="schedulerBuilder")
	private ISchedulerBuilder schedulerBuilder;
	@Override
	public void saveSchedule(TbZmScheduleJob po) throws BaseException,
			Exception {
		if(null!=po&&!"".equals(po)){
			po.setJobId(UUID.randomUUID().toString());
			po.setStatus("0");
			baseDao.save(po);
			schedulerBuilder.addCronTriggerJob(po);
		}else{
			throw new BaseException();
		}
	}
	@Override
	public void updateSchedule(TbZmScheduleJob po) throws BaseException,
			Exception {
		baseDao.update(TbZmScheduleJob.class, po, false);
		schedulerBuilder.upateJob(po);
	}
	@Override
	public void deleteSchedule(String[] ids) throws BaseException, Exception {
		for(String id:ids){
			TbZmScheduleJob scheduleJob =getSchedule(id);
			schedulerBuilder.delJob(scheduleJob);
			baseDao.delete(TbZmScheduleJob.class, id);
		}
	}
	@Override
	public TbZmScheduleJob getSchedule(String id) throws BaseException,
			Exception {
		return baseDao.getById(TbZmScheduleJob.class, id);
	}
	@Override
	public PageInfo<TbZmScheduleJobVO> getTbZmScheduleJobByPage(Page page,String jobName,String jobGroup) throws BaseException, Exception {
		PageHelper.startPage(page.getPageNum(),page.getPageSize());
		com.github.pagehelper.Page<TbZmScheduleJobVO> jobs=scheduleDao.getSchedules(page, jobName, jobGroup);
		PageInfo<TbZmScheduleJobVO> pageInfo=new PageInfo<TbZmScheduleJobVO>(jobs);
		if(!StringUtils.isEmpty(pageInfo.getList())){
			TbZmScheduleJob scheduleJob=null;
			for(TbZmScheduleJobVO vo:pageInfo.getList()){
				scheduleJob=new TbZmScheduleJob();
				scheduleJob.setJobName(vo.getJobName());
				scheduleJob.setJobGroup(vo.getJobGroup());
				vo.setTriggerStauts(schedulerBuilder.getTriggerStatusByJobName(scheduleJob));
			}
		}
		pageInfo.getList();
		return pageInfo;
	}
	@Override
	public List<TbZmScheduleJob> getAllTbZmScheduleJob() throws BaseException, Exception {
		return scheduleDao.getAllSchedules();
	}
	@Override
	public void updateJobStatusByJobName(String status, String jobName)
			throws BaseException, Exception {
		scheduleDao.updateJobStatusByJobName(status, jobName);
	}
	@Override
	public void saveTbZmScheduleJobLog(TbZmScheduleJobLog log)
			throws BaseException, Exception {
		if(null!=log&&!"".equals(log)){
			baseDao.save(log);
		}else{
			throw new BaseException();
		}
		
	}
	@Override
	public void stopSchedule(TbZmScheduleJob job) throws BaseException,
			Exception {
		schedulerBuilder.stopJob(job);
		job.setStatus("1");
		baseDao.update(TbZmScheduleJob.class, job, false);
	}
	
	@Override
	public void startSchedule(TbZmScheduleJob job) throws BaseException,
			Exception {
		schedulerBuilder.addCronTriggerJob(job);
	}
	@Override
	public void delJobLogFiveDayAgo() throws BaseException, Exception {
		scheduleDao.delJobLogFiveDayAgo();
	}
	@Override
	public TbZmScheduleJob getJobByJobNameAndJobGroup(String jobName,
			String jobGroup) throws BaseException, Exception {
		return scheduleDao.getJobByJobNameAndJobGroup(jobName, jobGroup);
	}

}
